package com.zoewin.zephyr.reportjasper.factory.creator;

import com.zoewin.zephyr.reportjasper.entity.ReportEntity;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

import java.sql.Connection;
import java.util.List;
import java.util.Map;

/**
 * Created by WQL on 2019/12/18
 * CEASE TO STRUGGLE AND YOU CEASE TO LIVE
 */
public class JasperPrintCreator {

    /**
     * 根据map键值对将数据填充到模板里面
     * @param reportEntity
     * @param map
     * @return
     */
    public static JasperPrint getJasperPrint(ReportEntity reportEntity, Map map ) throws Exception {
        map.putAll(reportEntity.getPicPath());
        try {
            JasperPrint jasperPrint = JasperFillManager.fillReport(reportEntity.getJasperReport(), map,new JREmptyDataSource());
            return jasperPrint;
        } catch (JRException e) {
            e.printStackTrace();
            throw new Exception("数据填充至模板失败! 失败原因: "+ e.toString());
        }

    }


    /**
     * 根据map键值对，以及list集合循环体填充模板
     * @param reportEntity
     * @param map
     * @param list
     * @return
     */
    public static JasperPrint getJasperPrint(ReportEntity reportEntity, Map map , List list) throws Exception {
        JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(list);
        map.putAll(reportEntity.getPicPath());
        try {
            JasperPrint jasperPrint = JasperFillManager.fillReport(reportEntity.getJasperReport(),map, dataSource);
            return jasperPrint;
        } catch (JRException e) {
            e.printStackTrace();
            throw new Exception("数据填充至模板失败! 失败原因: "+ e.toString());
        }
    }

    /**
     * 根据map键值对，以及数据源填充模板
     * @param reportEntity
     * @param map
     * @param connection
     * @return
     */
    public static JasperPrint getJasperPrint(ReportEntity reportEntity, Map map , Connection connection) throws Exception {
        map.putAll(reportEntity.getPicPath());
        try {
            JasperPrint jasperPrint = JasperFillManager.fillReport(reportEntity.getJasperReport(), map,connection);
            return jasperPrint;
        } catch (JRException e) {
            e.printStackTrace();
            throw new Exception("数据填充至模板失败! 失败原因: "+ e.toString());
        }
    }

    /**
     *该方法是将数据源放入到map里面，然后再jrxml模板里面关联数据源将数据赋值给table表格
     * @param reportEntity
     * @param map
     * @param ListName
     * @param list
     * @return
     */
    public static JasperPrint getJasperPrint(ReportEntity reportEntity, Map map , String ListName, List list) throws Exception {
        JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(list);
        map.putAll(reportEntity.getPicPath());
        map.put(ListName,dataSource);
        try {
            JasperPrint jasperPrint = JasperFillManager.fillReport(reportEntity.getJasperReport(), map,new JREmptyDataSource());
            return jasperPrint;
        } catch (JRException e) {
            e.printStackTrace();
            throw new Exception("数据填充至模板失败! 失败原因: "+ e.toString());
        }
    }
}
